package com.xrui.hbase.codec.impl;


import com.xrui.hbase.impl.BoundColumnReaderSpec;
import com.xrui.hbase.schema.TableLayout;
import com.xrui.hbase.schema.spec.CellSpec;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.io.DatumReader;

import java.io.IOException;

/**
 * Decodes cells encoded using Avro into generic types.
 *
 * @param <T> The type of the decoded data.
 */
public final class GenericAvroCellDecoder<T> extends AvroCellDecoder<T> {
    /**
     * Initializes a cell decoder that creates generic Avro types.
     *
     * @param cellSpec Specification of the cell encoding.
     * @throws IOException on I/O error.
     */
    public GenericAvroCellDecoder(CellSpec cellSpec) throws IOException {
        super(cellSpec);
    }

    /**
     * Initializes a cell decoder that creates generic Avro types.
     *
     * @param layout TableLayout from which to create storage information.
     * @param spec   Specification of the cell encoding.
     * @throws IOException on I/O error.
     */
    public GenericAvroCellDecoder(TableLayout layout, BoundColumnReaderSpec spec) throws IOException {
        super(layout, spec);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected DatumReader<T> createDatumReader(Schema writer, Schema reader) {
        return new GenericDatumReader<>(writer, reader);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getName() {
        return GenericAvroCellDecoder.class.getName();
    }
}
